//
//  XFYRobot+Brain.m
//  MyRobotApp
//
//  Created by 小肥羊 on 15/10/12.
//  Copyright © 2015年 小肥羊. All rights reserved.
//

#import "XFYRobot+Brain.h"

@implementation XFYRobot (Brain)

#pragma mark - 处理
- (void)receivingInformationWithMessage:(NSString *)message
{
    if (message && ![message isEqualToString:@""]) {
        NSLog(@"接收到消息:%@",message);
//        [self.memory remember:message];
        NSString *lastWord = [message substringFromIndex:message.length - 1];
        if ([lastWord isEqualToString:@"?"] || [lastWord isEqualToString:@"？"])
        {
            //疑问句
            NSLog(@"对方在问问题");
            [self questionAnalysis:message];
        } else if ([lastWord isEqualToString:@"@"]) {
            NSLog(@"主人教我");
            [self teachCogniton:message];
        }
//        //接受信息后的处理，目前测试
//        if ([message isEqualToString:@"hello"]) {
//            [self idea:@"你好呀～"];
//            [self moodExposed:MoodAccept];
//        } else if ([message isEqualToString:@"fuck"])
//        {
//            [self idea:@"fuck you!"];
//            [self moodExposed:MoodRage];
//        } else
//        {
//            [self idea:[NSString stringWithFormat:@"%@?",message]];
//            [self moodExposed:MoodNormal];
//        }
    }
}

#pragma mark - 语义分析
- (void)questionAnalysis:(NSString *)message
{
    NSString *content = [message substringToIndex:message.length - 1];
    NSArray *wordArray = [content componentsSeparatedByString:@"是"];
    //开始判断问句
    if ((wordArray.count == 2) && ![wordArray[1] isEqualToString:@""]) {
        NSString *subjectWord = wordArray[0];
        NSString *objectWord = wordArray[1];
        NSLog(@"%@,%@",subjectWord,objectWord);
        //主语判断
        if ([subjectWord isEqualToString:@"你"]) {
            subjectWord = @"我";
        }
        //判断 认知
        NSString *cognition = [self hasCognition:subjectWord];
        if (cognition) {
            NSLog(@"我想我有答案，容我再思考片刻");
            //宾语判断
            if ([objectWord isEqualToString:@"谁"] || [objectWord isEqualToString:@"什么"]) {
                [self idea:@"疑问句"];
            } else {
                [self idea:@"判断句"];
            }
        } else {
            [self idea:[NSString stringWithFormat:@"%@是什么？",subjectWord]];
            return;
        }
    }
}

- (NSString *)hasCognition:(NSString *)category
{
    NSArray *cognitionArray = [self.memory AllCognition];
    for (NSDictionary *data in cognitionArray) {
        if ([data[@"Cognition"] isEqualToString:category]) {
            return data[@"CognitionTableName"];
        }
    }
    return nil;
}

#pragma mark - 传递知识
- (void)teachCogniton:(NSString *)message
{
    NSString *content = [message substringToIndex:message.length - 1];
    NSArray *wordArray = [content componentsSeparatedByString:@"是"];
    //开始传递知识
    if ((wordArray.count == 2) && ![wordArray[1] isEqualToString:@""]) {
        NSString *subjectWord = wordArray[0];
        NSString *objectWord = wordArray[1];
        NSLog(@"%@,%@",subjectWord,objectWord);
        //主语判断
        if ([subjectWord isEqualToString:@"你"]) {
            subjectWord = @"我";
        }
        //判断 认知
        NSString *cognition = [self hasCognition:subjectWord];
        if (cognition) {
            NSLog(@"要在%@,添加%@",cognition,objectWord);
        } else {
            NSLog(@"先创建%@认知,再添加%@",subjectWord,objectWord);
        }
        [self idea:[NSString stringWithFormat:@"%@是%@",subjectWord,objectWord]];
    }
//    if (judge) {
//        NSLog(@"在%@表里添加%@为是",cognition,category);
//    } else {
//        NSLog(@"在%@表里添加%@为不是",cognition,category);
//    }
}

#pragma mark - 情绪外露
- (void)moodExposed:(MoodType)mood
{
    self.state.mood = mood;
    [self.appearance showMood:mood];
}

@end
